package university;
import university.exceptions.*;

import java.util.*;

/**
 * Class that describes retry of exam
 * @author anatolii.bondarenko.
 */
class RetryExam extends Exam {
    private int numberOfRetries;
    private List<Student> studentsForRetry;
    /**
     * Details of exam retry
     * @param d is discipline of exam(teacher is taken from discipline class)
     * @param sfr is list of students for retry of exam
     * @param ev is time and place of exam retry
     * @param retr is number of retry(if student fails 3 retries he is resigned)
     */
    public RetryExam(Discipline d, List<Student> sfr, ExamEvent ev, int retr) throws ListEmptyException, InvalidNumberOfRetriesException{
        super(d,ev);
        numberOfRetries = retr;
        studentsForRetry = sfr;
        if (sfr.size() == 0){
            throw new ListEmptyException("No Students for this retry");
        }
        if (retr < 1 || retr > 3){
            throw new InvalidNumberOfRetriesException("Retry number can be from 1 to 3");
        }
    }

    /**
     * Returns details of exam retry in string format
     * @return exam retry details
     */
    public String toString(){
        String s = "Exam retry for discipline" + super.getDiscipline() + super.getTimeAndPlace() + ". Teacher: "
                + super.getTeacher() + ". For Students: ";
        for(int i = 0; i< studentsForRetry.size(); i++){
            s = s + studentsForRetry.get(i).getName()+", ";
        }
        s = s.trim();
        s = s + ".";
        return s;
    }

    /**
     * If student passes the exam his number of failures reduces and he is removed from list that failed this exam.
     * If student fails the exam he remains in list of students that failed the exam.
     * If student had failed 3 retries he is resigned.
     * @param mark is assessment of the exam
     * @param s is the student that is currently being examined
     */
    public void examine(int mark, Student s) throws InvalidMarkException, ExamineResignedStudentException, NoElementInListException, NoFailedDisciplinesException{
        if (!studentsForRetry.contains(s)){
            throw new NoElementInListException("No such student for retry");
        }
        if (s.isResigned()==true){
            throw new ExamineResignedStudentException("Cannot examine resigned students");
        }
        if (mark >= 1 && mark <=5){
            if (mark >2){
                s.reduceFailedDisciplines();
                studentsForRetry.remove(s);
            }
            else {
                if (numberOfRetries>=3){
                    s.resign();
                }
            }
        }
        else {
            throw new InvalidMarkException("Exam marks must be from 1 to 5");
        }

    }

    /**
     * Returns the list of students that failed the exam
     * @return list of students that failed the exam
     */
    public List<Student> getStudentsForRetry(){
        return studentsForRetry;
    }


}
